﻿Imports System.Windows.Forms
Public Class frmsearch
    Public StrItemNo As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmsearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frmmain.mSpk.Play("ReadContinued")

        Me.TxtProductname.Focus()
    End Sub

    Private Sub TxtProductname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtProductname.KeyDown
        If e.KeyCode = Keys.Escape Then Me.Dispose()
        If e.KeyCode = Keys.Enter Then
            If Me.griditem.RowCount = 0 Then
                Me.TxtProductname.Select(0, TxtProductname.Text.ToString.Length)
                Exit Sub
            End If

            StrItemNo = griditem.Item(0, 0).Value.ToString
            Me.Hide()
        End If
    End Sub

    Private Sub TxtProductname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtProductname.KeyPress
        
    End Sub

    Private Sub TxtProductname_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TxtProductname.MouseClick
        Me.TxtProductname.Select(0, TxtProductname.Text.ToString.Length)
    End Sub

    Private Sub TxtProductname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtProductname.TextChanged
        Try
            Dim Mysql1 As New ClsSQLhelper
            Dim DS As New DataSet
            Dim Strsql As String = ""
            Dim strCeteria As String = ""
            Dim strCeteria2 As String = ""

            If Me.TxtProductname.Text.Trim = " " Then strCeteria2 = "%"
            Select Case Me.OptProductname.Checked
                Case True
                    If Me.TxtProductname.Text.Trim <> "" Then
                        strCeteria = " where itemname like '" & Me.TxtProductname.Text.Trim & "%' and siteid = " & My.Settings.CurrentSite & " order by itemname limit " & Me.NUPLimit.Value
                    Else
                        strCeteria = " where itemname = ''"
                    End If
                Case False
                    If Me.TxtProductname.Text.Trim <> "" Then
                        strCeteria = " where alternatenumber like '" & Me.TxtProductname.Text.Trim & "%' and siteid = " & My.Settings.CurrentSite & " order by alternatenumber limit " & Me.NUPLimit.Value
                    Else
                        strCeteria = " where alternatenumber = ''"
                    End If
            End Select




            Strsql = "select itemnumber as 'ID' , itemname as 'Name',alternatenumber as 'Barcode' from item " & strCeteria
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Item", "item")
            Debug.Print(Strsql)
            With griditem
                .DataSource = DS.Tables(0)
                .BackgroundColor = Color.LemonChiffon
                .AllowUserToAddRows = False
                .RowHeadersVisible = False
                .AllowUserToResizeRows = False
                .AllowUserToResizeColumns = False
                .ReadOnly = True
                ' .Columns(0).HeaderText = "ลำดับ"
                '.Columns(1).HeaderText = "ประเภท"

                If .Rows.Count = 1 Then
                    StrItemNo = griditem.Item(2, 0).Value.ToString
                    Me.Hide()
                End If

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub griditem_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griditem.CellDoubleClick
        Try
            StrItemNo = griditem.Item(2, e.RowIndex).Value.ToString
            Me.Hide()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub OK_Button_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If griditem.RowCount = 0 Then Exit Sub
        StrItemNo = griditem.Item(2, 0).Value.ToString
        Me.Hide()
    End Sub

    Private Sub Cancel_Button_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.Dispose()
    End Sub

    Private Sub OptBarcode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptBarcode.CheckedChanged
        Me.TxtProductname.Text = ""
        Me.TxtProductname.Focus()
    End Sub

    Private Sub OptProductname_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptProductname.CheckedChanged
        Me.TxtProductname.Text = ""
        Me.TxtProductname.Focus()
    End Sub

    Private Sub griditem_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griditem.CellContentClick

    End Sub
End Class
